package com.example.final_project.controller;

import com.example.final_project.entity.Publisher;
import com.example.final_project.service.PublisherService;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Tag(name = "出版社管理", description = "出版社相关的API接口")
@Controller
@RequestMapping("/publisher")
public class PublisherController {

    @Autowired
    private PublisherService publisherService;

    @Operation(summary = "出版社列表页面", description = "返回出版社列表页面")
    @GetMapping("/list")
    public String list(Model model,
                      @Parameter(description = "出版社名称") @RequestParam(required = false) String name,
                      @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
                      @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Publisher> pageInfo = publisherService.getList(name, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("name", name);
        return "publisher/list";
    }

    @Operation(summary = "添加出版社页面", description = "返回添加出版社页面")
    @GetMapping("/add")
    public String add() {
        return "publisher/add";
    }

    @Operation(summary = "添加出版社", description = "添加新出版社")
    @ApiResponse(responseCode = "200", description = "添加成功返回success，失败返回error")
    @PostMapping("/add")
    @ResponseBody
    public String add(@Parameter(description = "出版社信息") @RequestBody Publisher publisher) {
        return publisherService.add(publisher) ? "success" : "error";
    }

    @Operation(summary = "编辑出版社页面", description = "返回编辑出版社页面")
    @GetMapping("/edit/{id}")
    public String edit(@Parameter(description = "出版社ID") @PathVariable Long id, Model model) {
        Publisher publisher = publisherService.getById(id);
        model.addAttribute("publisher", publisher);
        return "publisher/edit";
    }

    @Operation(summary = "更新出版社", description = "更新出版社信息")
    @ApiResponse(responseCode = "200", description = "更新成功返回success，失败返回error")
    @PostMapping("/edit")
    @ResponseBody
    public String edit(@Parameter(description = "出版社信息") @RequestBody Publisher publisher) {
        return publisherService.update(publisher) ? "success" : "error";
    }

    @Operation(summary = "删除出版社", description = "根据出版社ID删除出版社")
    @ApiResponse(responseCode = "200", description = "删除成功返回success，失败返回error")
    @PostMapping("/delete/{id}")
    @ResponseBody
    public String delete(@Parameter(description = "出版社ID") @PathVariable Long id) {
        return publisherService.delete(id) ? "success" : "error";
    }

    @Operation(summary = "获取出版社详情", description = "根据出版社ID获取出版社详细信息")
    @ApiResponse(responseCode = "200", description = "成功获取出版社详情")
    @GetMapping("/detail/{id}")
    @ResponseBody
    public Map<String, Object> detailApi(@Parameter(description = "出版社ID") @PathVariable Long id) {
        Publisher publisher = publisherService.getById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("publisher", publisher);
        return result;
    }

    @Operation(summary = "出版社详情页面", description = "返回出版社详情页面")
    @GetMapping("/detailPage/{id}")
    public String detailPage(@Parameter(description = "出版社ID") @PathVariable Long id, Model model) {
        Publisher publisher = publisherService.getById(id);
        model.addAttribute("publisher", publisher);
        return "publisher/detail";
    }

    @Operation(summary = "获取所有出版社", description = "获取所有出版社列表")
    @ApiResponse(responseCode = "200", description = "成功获取所有出版社列表")
    @GetMapping("/all")
    @ResponseBody
    public List<Publisher> getAll() {
        return publisherService.getAll();
    }

    @Operation(summary = "获取出版社列表", description = "分页获取出版社列表数据")
    @ApiResponse(responseCode = "200", description = "成功获取出版社列表")
    @GetMapping("/api/list")
    @ResponseBody
    public Map<String, Object> apiList(
            @Parameter(description = "出版社名称") @RequestParam(required = false) String name,
            @Parameter(description = "页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        PageInfo<Publisher> pageInfo = publisherService.getList(name, pageNum, pageSize);
        Map<String, Object> result = new HashMap<>();
        result.put("list", pageInfo.getList());
        result.put("total", pageInfo.getTotal());
        return result;
    }
} 